﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RaProWorld
{
    public class WorldMap
    {
        protected Texture2D background;
        protected Rectangle viewportRect;

        protected List<Vector2> objectPositions;
        public List<WorldmapObject> objects;
        public int currentSelectedIndex = 0;
        public SubMenu1 subMenu;

        public List<Textbox> textBoxes;

        public WorldMap(ContentManager contentManager, Rectangle viewport)
        {
            background = contentManager.Load<Texture2D>("Worldmap\\Sprites\\map_1_0");
            viewportRect = viewport;

            objectPositions = new List<Vector2>();
            objectPositions.Add(new Vector2(100, 250));
            objectPositions.Add(new Vector2(600, 250));

            objects = new List<WorldmapObject>();

            textBoxes = new List<Textbox>();
        }

        public void Update()
        {
            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Update();
                if (i == currentSelectedIndex)
                {
                    objects[i].isSelected = true;
                    continue;
                }
                objects[i].isSelected = false;

            }
            if (Program.raProWorld.GameState == "SUBMENU" || Program.raProWorld.GameState == "TEXTBOX")
            {
                subMenu.Update();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, viewportRect, Color.White);
            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Draw(spriteBatch);
            }
            if (Program.raProWorld.GameState == "SUBMENU" || Program.raProWorld.GameState == "TEXTBOX")
            {
                subMenu.Draw(spriteBatch);
            }
            for (int i = 0; i < textBoxes.Count; i++)
            {
                textBoxes[i].Draw(spriteBatch);
            }
            
        }

        public void addObject(Texture2D sprite, RaProGame game, String description,Score score,String name)
        {
            if (objectPositions.Count <= 0) return;

            objects.Add(new WorldmapObject(sprite, objectPositions[0], game, description,score,name));
            objectPositions.RemoveAt(0);
        }

        public void addTextBox(String text)
        {
            if (textBoxes.Count == 0)
            {
                textBoxes.Add(new Textbox(text));
            }
            else
            {
                textBoxes.Add(new Textbox(
                    textBoxes[textBoxes.Count - 1].Boundaries.X + 32,
                    textBoxes[textBoxes.Count - 1].Boundaries.Y + 32,
                    textBoxes[textBoxes.Count - 1].Boundaries.Width - 32,
                    textBoxes[textBoxes.Count - 1].Boundaries.Height,
                    text));
            }
        }

        public void addSubmenu() 
        {
            subMenu = new SubMenu1(objects[currentSelectedIndex].pos, objects[currentSelectedIndex].score, objects[currentSelectedIndex].name);
        }

        public void removeTextBox()
        {
            if (textBoxes.Count == 0) return;
            textBoxes.RemoveAt(textBoxes.Count - 1);

            if (textBoxes.Count == 0) Program.raProWorld.GameState = "SUBMENU";
        }
    }
}